package com.example.dormitorymanagement.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.dormitorymanagement.entity.Student;
import com.example.dormitorymanagement.exception.AbnormalPermissions;
import com.example.dormitorymanagement.mapper.StudentMapper;
import com.example.dormitorymanagement.service.impl.StudentServiceImpl;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;












import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author admin
 * @since 2021-05-28
 */
//@CrossOrigin
@RestController
@RequestMapping("/student")
@Api(tags = "学生管理，权限低：数据的查询，权限高：数据的增加，修改，查询，删除")
public class StudentController {

    @Autowired
    private StudentServiceImpl studentserviceimpl;

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private com.example.dormitorymanagement.service.impl.suthorityservice suthorityservice;


    @PostMapping(value = "/addormodify")
    @ApiOperation(value = "学生的添加或者修改")
    public Map<String, Object> addormodify(@RequestBody Student student, HttpServletRequest request) throws AbnormalPermissions {

        System.out.println("进入添加学生的方法：" + student);

        Map<String, Object> map = suthorityservice.verification(request);

        if (map.size() != 0)
        {
            return map;
        }


        if (student.getId() != null && student.getId() != 0)//id不为空就是进行修改
        {

           if (studentserviceimpl.updateById(student)) {

                map.put("stat",true);
                map.put("message","更新成功，本次更新的数据：" + student);

           }else {

                map.put("stat",false);
               map.put("message","更新失败");

            }

            return map;
        }

        //添加学生前，先查看一下数据库中是否有该学生
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();

        queryWrapper.eq("student_id", student.getStudentId());

        Integer integer = studentMapper.selectCount(queryWrapper);

        if (integer == 0) {

            System.out.println("学识是否存在：" + integer);

            boolean save = studentserviceimpl.save(student);

            System.out.println("是否添加成功：" + save);

            if (save) {

                map.put("stat", true);

                map.put("message", "添加成功");

                return map;
            }

            map.put("stat", false);

            map.put("message", "添加失败，请联系管理员");

            return map;

        }

        map.put("stat", false);
        map.put("message", "已存在" + student.getStudentId() + "该学生");

        return map;


    }

    @GetMapping(value = "/getstudent")
    @ApiOperation(value = "根据条件查询获得数据，一页默认10条数据")

    public Map<String, Object> getstudent(@ApiParam(name = "condition",value = "条件可为学号，姓名，年龄，性别，电话，宿舍号，班级号任意一项，不是必传选项") @RequestParam(value = "condition",required = false) String condition,
                                          @ApiParam(name = "pc",value = "页数，不传值默认是第一页") @RequestParam(value = "pc",required = false) Integer pc)
    {

        Map<String, Object> map = new HashMap<>();
        if (pc == null)
        {
            pc = 1;
        }

        Page<Student> studentPage = new Page<>(pc,10);

        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();

        if (condition != null)
        {
            //长度为13的是学号
            if ( condition.length() == 13)
            {
                studentQueryWrapper.eq("student_id",condition);

            }else if (condition.length() == 4 || condition.length() == 3)//长度为3或者4的是姓名
            {
                studentQueryWrapper.eq("student_name",condition);

            }else if (condition.length() == 2)//长度为2是年龄
            {
                studentQueryWrapper.eq("student_age",condition);

            }else if (condition.length() == 1)//长度为1是性别
            {
                studentQueryWrapper.eq("student_sex",condition);

            }else if (condition.length() == 11)//长度为11是手机号
            {
                studentQueryWrapper.eq("student_phone",condition);

            }else if (condition.contains("栋")){//包含栋的是查宿舍楼

                studentQueryWrapper.eq("student_dormitory",condition);
            }
            else if (condition.contains("班")){//包含栋的是查宿舍楼

                studentQueryWrapper.eq("student_class",condition);
            }
        }

        studentMapper.selectPage(studentPage,studentQueryWrapper);

        if (pc > studentPage.getPages())
        {
            map.put("stat",false);
            map.put("message","页码越界,总页数：" + studentPage.getPages() + "，当前页数：" + studentPage.getCurrent());
            return map;
        }

        List<Student> records = studentPage.getRecords();

        map.put("stat",true);
        map.put("studetnlist", records);
        map.put("pages",studentPage.getPages());
        map.put("page",studentPage.getCurrent());
        map.put("total", studentPage.getTotal());
        map.put("current", studentPage.getCurrent());
        map.put("hasPrevious",studentPage.hasPrevious());
        map.put("hasNext",studentPage.hasNext());
        map.put("condition",condition);
        return map;
    }

    @GetMapping(value = "/deletestudent")
    @ApiOperation(value = "根据学号来删除对应的学生")
    public Map<String,Object> deletestudent(@ApiParam(name = "studentId",value = "学生的学号",type = "String",required = true) @RequestParam(value = "studentId") String studentId,
                                            HttpServletRequest request) throws AbnormalPermissions {
        Map<String, Object> map = suthorityservice.verification(request);

        if (map.size() != 0)
        {
            return map;
        }


        UpdateWrapper<Student> studentUpdateWrapper = new UpdateWrapper<>();

        studentUpdateWrapper.eq("student_id",studentId);

        int result = studentMapper.delete(studentUpdateWrapper);

        if (result == 0)
        {

            map.put("stat",false);
            map.put("message","删除失败");

        }else {

            map.put("stat",true);
            map.put("message","删除成功");

        }

        return map;

    }


}





























